package com.yaopaikeji.dao;

import com.alibaba.fastjson.JSONObject;
import com.yaopaikeji.constants.Constants;
import com.yaopaikeji.exception.DeleteException;
import com.yaopaikeji.exception.InsertException;
import com.yaopaikeji.exception.UpdateException;
import com.yaopaikeji.mapper.EmailGroupMapper;
import com.yaopaikeji.model.EmailGroupInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class EmailGroupDao extends BaseDao<EmailGroupInfo> {

    private static final Logger logger = LoggerFactory.getLogger(EmailGroupDao.class);

    @Autowired
    private EmailGroupMapper emailGroupMapper;

    @Override
    public EmailGroupInfo findByPrimaryKey(int id) {
        return emailGroupMapper.findGroupByPrimaryKey(id);
    }

    @Override
    public List<EmailGroupInfo> findAll() {
        return emailGroupMapper.findGroupsAll();
    }

    public EmailGroupInfo addNewGroup(EmailGroupInfo emailGroupInfo){

        emailGroupMapper.insertEmailGroup(emailGroupInfo);
        return emailGroupInfo;
    }

    public EmailGroupInfo updateGroupInfo(EmailGroupInfo emailGroupInfo){

       return emailGroupMapper.updateEmailGroup(emailGroupInfo);
    }

    public boolean deleteGroupById(int id){

        int rows = emailGroupMapper.deleteEmailGroup(id);
        if(rows < 1) {

            logger.error("删除邮件组出错，id="+id);
            throw new DeleteException(String.format(Constants.DELETE_ERROR_FMT, "邮件组"));
        }

        return true;
    }
}
